package com.offics.loan.domain.service;

import com.offics.loan.domain.entity.FAQ;
import com.offics.loan.domain.repository.FAQRepository;
import com.offics.loan.infrastructure.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * FAQ领域服务
 */
@Service
@RequiredArgsConstructor
public class FAQDomainService {

    private final FAQRepository faqRepository;

    /**
     * 获取所有启用的FAQ列表
     *
     * @return FAQ列表
     */
    public List<FAQ> getAllEnabledFAQs() {
        return faqRepository.findAllEnabled();
    }

    /**
     * 根据分类获取启用的FAQ列表
     *
     * @param category 分类
     * @return FAQ列表
     */
    public List<FAQ> getFAQsByCategory(Integer category) {
        if (category == null || category < 0 || category > 3) {
            throw new BusinessException("无效的FAQ分类");
        }
        return faqRepository.findEnabledByCategory(category);
    }

    /**
     * 根据ID获取FAQ详情
     *
     * @param id FAQ ID
     * @return FAQ详情
     */
    public FAQ getFAQById(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException("无效的FAQ ID");
        }
        
        Optional<FAQ> faqOptional = faqRepository.findById(id);
        if (faqOptional.isEmpty()) {
            throw new BusinessException("FAQ不存在");
        }
        
        FAQ faq = faqOptional.get();
        if (!faq.isEnabled()) {
            throw new BusinessException("FAQ已禁用");
        }
        
        return faq;
    }

    /**
     * 创建FAQ
     *
     * @param faq FAQ
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean createFAQ(FAQ faq) {
        if (faq == null) {
            throw new BusinessException("FAQ信息不能为空");
        }
        
        validateFAQ(faq);
        
        // 设置默认状态为启用
        if (faq.getStatus() == null) {
            faq.setStatus(1);
        }
        
        // 设置默认排序
        if (faq.getSort() == null) {
            faq.setSort(0);
        }
        
        return faqRepository.save(faq);
    }

    /**
     * 更新FAQ
     *
     * @param faq FAQ
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateFAQ(FAQ faq) {
        if (faq == null || faq.getId() == null) {
            throw new BusinessException("FAQ信息不完整");
        }
        
        // 检查FAQ是否存在
        Optional<FAQ> existingFAQ = faqRepository.findById(faq.getId());
        if (existingFAQ.isEmpty()) {
            throw new BusinessException("FAQ不存在");
        }
        
        validateFAQ(faq);
        
        return faqRepository.update(faq);
    }

    /**
     * 删除FAQ
     *
     * @param id FAQ ID
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFAQ(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException("无效的FAQ ID");
        }
        
        // 检查FAQ是否存在
        Optional<FAQ> existingFAQ = faqRepository.findById(id);
        if (existingFAQ.isEmpty()) {
            throw new BusinessException("FAQ不存在");
        }
        
        return faqRepository.deleteById(id);
    }

    /**
     * 验证FAQ数据
     *
     * @param faq FAQ
     */
    private void validateFAQ(FAQ faq) {
        if (faq.getTitle() == null || faq.getTitle().trim().isEmpty()) {
            throw new BusinessException("FAQ标题不能为空");
        }
        
        if (faq.getContent() == null || faq.getContent().trim().isEmpty()) {
            throw new BusinessException("FAQ内容不能为空");
        }
        
        if (faq.getCategory() == null || faq.getCategory() < 0 || faq.getCategory() > 3) {
            throw new BusinessException("无效的FAQ分类");
        }
        
        if (faq.getTitle().length() > 100) {
            throw new BusinessException("FAQ标题长度不能超过100个字符");
        }
    }
}